import Vue from 'vue'
import VueRouter from 'vue-router'
import UserLogin from '@/views/User/UserLogin.vue'
import LayoutPage from '@/views/Layout/LayoutPage.vue'
import ArticleList from '@/views/Home/ArticleList.vue'
import ArticleSearch from '@/views/Home/ArticleSearch.vue'
import SearchResult from '@/views/Home/SearchResult.vue'
import ChannelEdit from '@/views/Home/ChannelEdit.vue'
import ArticleDetail from '@/views/Home/ArticleDetail.vue'
import FollowPage from '@/views/Follow/FollowPage.vue'
import CollectPage from '@/views/Collect/CollectPage.vue'
import UserPage from '@/views/User/UserPage.vue'
import UserEdit from '@/views/User/UserEdit.vue'
import UserStudent from '@/views/User/UserStudent.vue'
import notfound from '@/views/notfound/notfound.vue'

// 导入token工具包
import { getToken } from '@/utils/token.js'
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    name: 'UserLogin',
    path: '/UserLogin',
    component: UserLogin
  },
  {
    name: 'LayoutPage',
    path: '/',
    component: LayoutPage,
    children: [
      {
        path: '/',
        redirect: '/ArticleList'
      },
      {
        path: '/ArticleList',
        component: ArticleList
      },
      {
        name: 'FollowPage',
        path: '/FollowPage',
        component: FollowPage
      },
      {
        name: 'CollectPage',
        path: '/CollectPage',
        component: CollectPage
      },
      {
        name: 'UserPage',
        path: '/UserPage',
        component: UserPage
      }
    ]
  },
  {
    name: 'ArticleDetail',
    path: '/ArticleDetail',
    component: ArticleDetail
  },
  {
    name: 'UserEdit',
    path: '/UserEdit',
    component: UserEdit
  },
  {
    path: '/notfound',
    component: notfound,
    meta: { showTab: true }
  }, {
    name: 'UserStudent',
    path: '/UserStudent',
    component: UserStudent
  }, {
    name: 'ArticleSearch',
    path: '/ArticleSearch',
    component: ArticleSearch
  }, {
    name: 'SearchResult',
    path: '/SearchResult',
    component: SearchResult
  }, {
    name: 'ChannelEdit',
    path: '/ChannelEdit',
    component: ChannelEdit
  },
  {
    path: '*',
    redirect: '/notfound'
  }
]

/* 去掉路由跳转reject异常提示
注意：我们是直接修改VueRouter的原型，所以需要在实例化之前调用
*/
// (1)先保存路由原型链中默认的跳转函数
const originalPush = VueRouter.prototype.push
// (2)替换原型链中的push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) {
    // 正常执行
    // originalPush.call() : 我们自定义的push底层还是调用默认的push
    return originalPush.call(this, location, onResolve, onReject)
  }
  // 如果有异常，本来这个err默认是抛出的，我们直接不做任何处理（给吞掉了）
  /*
  底层默认是抛出异常：err => {throw err}
  我们不抛出，不做任何处理：err => err
  */
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})

// 注册导航守卫 (全局前置守卫  跳转之前执行)
router.beforeEach((to, from, next) => {
  // 判断用户没有没有登录 或  是不是跳转登录页
  if (getToken() || to.path === '/UserLogin') {
    // 正常跳转
    next()
  } else {
    /* this.$toast只能在组件里面使用,全局使用直接Toast即可（官网文档有介绍） */
    Toast.fail('请先登录')
    // 没有登录，则跳转登录页
    next({ path: '/UserLogin' })
  }
})

export default router
